package com.example.sys.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.config.ResponseBean;
import com.example.config.UnicomResponseEnums;
import com.example.sys.entity.Menu;
import com.example.sys.service.MenuService;
import com.example.sys.service.RoleMenuService;
import com.example.sys.service.RoleService;
import com.example.sys.vo.IPageVo;
import com.example.sys.vo.MenuVo;
import com.example.util.BaseUtils;
import com.example.util.VoUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 菜单权限表 前端控制器
 * </p>
 *
 * @author anan
 * @since 2019-06-20
 */
@RestController
@RequestMapping("/sys/menu")
public class MenuController {

    @Autowired
    private MenuService menuService;

    @Autowired
    private RoleMenuService roleMenuService;

    @Autowired
    private RoleService roleService;

    @PostMapping("/createMenu")
    @RequiresPermissions({"system:menu:add"})
    public ResponseBean<UnicomResponseEnums> createMenu(@RequestBody Menu menu){
        if (!menu.toString().isEmpty()) {

            menuService.save(menu);
            return new ResponseBean(true,UnicomResponseEnums.SUCCESS_OPTION);
        }
        return new ResponseBean(false,UnicomResponseEnums.OPERATION_FAILED);
    }

    @DeleteMapping("/delMenuById")
    @RequiresPermissions({"system:menu:remove"})
    public ResponseBean<UnicomResponseEnums> delMenuById(Long menuId){
        if (!menuId.toString().isEmpty()) {
            menuService.removeById(menuId);
            return new ResponseBean(true, UnicomResponseEnums.SUCCESS_OPTION);
        }
        return new ResponseBean(false, UnicomResponseEnums.OPERATION_FAILED);
    }

    @DeleteMapping("/deleteMenus")
    @RequiresPermissions({"system:menu:remove"})
    public ResponseBean<UnicomResponseEnums> deleteMenus(List<Long> menuIds){
        if (!menuIds.isEmpty()) {
            for (Long menuId:menuIds) {
                delMenuById(menuId);
            }
            return new ResponseBean(true, UnicomResponseEnums.SUCCESS_OPTION);
        }
        return new ResponseBean(false, UnicomResponseEnums.OPERATION_FAILED);
    }

    @PostMapping("/updateMenuById")
    @RequiresPermissions({"system:menu:edit"})
    public ResponseBean<UnicomResponseEnums> updateMenuById(@RequestBody Menu menu){
        if (!menu.toString().isEmpty()) {
            menuService.updateById(menu);
            return new ResponseBean(true,UnicomResponseEnums.SUCCESS_OPTION);
        }
        return new ResponseBean(false,UnicomResponseEnums.OPERATION_FAILED);
    }

    @RequestMapping("/updateMenus")
    @RequiresPermissions({"system:menu:edit"})
    public ResponseBean<UnicomResponseEnums> updateMenus(@Valid List<Menu> menus){
        if (!menus.isEmpty()) {
            for (Menu menu:menus) {
                updateMenuById(menu);
            }
            return new ResponseBean(true, UnicomResponseEnums.SUCCESS_OPTION);
        }
        return new ResponseBean(false, UnicomResponseEnums.OPERATION_FAILED);
    }

    @RequestMapping("/getMenu")
    @RequiresPermissions({"system:menu:list"})
    public ResponseBean<UnicomResponseEnums> getMenu(Long menuId){
        if (!menuId.toString().isEmpty()) {
            Menu menu=menuService.getById(menuId);
            return new ResponseBean(true, menu, UnicomResponseEnums.SUCCESS_OPTION);
        }
        return new ResponseBean(false, UnicomResponseEnums.OPERATION_FAILED);
    }

    @RequestMapping("/getMenuList")
    public ResponseBean<UnicomResponseEnums> getMenuList(MenuVo menuVo){
        System.out.println(menuVo);
        Map<String, Object> stringObjectMap = BeanUtil.beanToMap(menuVo, false, true);
        List<Menu> menus=menuService.list();
        BaseUtils baseUtils=new BaseUtils();
        if (stringObjectMap.isEmpty()){
            return new ResponseBean(true, menus, UnicomResponseEnums.SUCCESS_OPTION);
        }
        if (menuVo.getParentId()==null){
            List<MenuVo> list =baseUtils.ArrangementMenuTree(menus, 0L);
            return new ResponseBean(true, list, UnicomResponseEnums.SUCCESS_OPTION);
        }
        QueryWrapper<Menu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id", menuVo.getParentId());
        IPage<Menu> iPageMenu=menuService.selectPage(menuVo);
        List<MenuVo> listMenus = new ArrayList<>();
        for (Menu m:iPageMenu.getRecords()) {
            listMenus.add(VoUtil.menuToMenuVo(m));
        }
        IPageVo<MenuVo> iPageVo = new IPageVo<>();
        System.out.println(iPageVo);

        iPageVo.setSize(iPageMenu.getSize());
        iPageVo.setCurrent(iPageMenu.getCurrent());
        iPageVo.setTotal(iPageMenu.getTotal());
        iPageVo.setPages(iPageMenu.getPages());
        List<MenuVo> menuVoList=baseUtils.ArrangementMenuTree(iPageMenu,menus);
        iPageVo.setRecords(menuVoList);
        return new ResponseBean(true, iPageVo, UnicomResponseEnums.SUCCESS_OPTION);
    }

    @RequestMapping("/getMenuListMC")
    public ResponseBean<UnicomResponseEnums> getMenuListMC(MenuVo menuVo){
        BaseUtils baseUtils=new BaseUtils();

//        获取除按钮外的全部菜单
        QueryWrapper<Menu> queryWrapper = new QueryWrapper<>();
        queryWrapper.ne("menu_type", "F");
        List<Menu> menus=menuService.list(queryWrapper);

//        查询父菜单
        IPage<Menu> iPageMenu=menuService.selectPage(menuVo);
        List<MenuVo> listMenus = new ArrayList<>();
//        将父菜单加入listMenus
        for (Menu m:iPageMenu.getRecords()) {
            listMenus.add(VoUtil.menuToMenuVo(m));
        }

        IPageVo<MenuVo> iPageVo = new IPageVo<>();
        iPageVo.setSize(iPageMenu.getSize());
        iPageVo.setCurrent(iPageMenu.getCurrent());
        iPageVo.setTotal(iPageMenu.getTotal());
        iPageVo.setPages(iPageMenu.getPages());
        List<MenuVo> menuVoList=baseUtils.ArrangementMenuTree(iPageMenu,menus);
        iPageVo.setRecords(menuVoList);
        return new ResponseBean(true, iPageVo, UnicomResponseEnums.SUCCESS_OPTION);
    }

    @RequestMapping("/getMenuLists")
    public ResponseBean<UnicomResponseEnums> getMenuLists(MenuVo menuVo){
        Map<String, Object> stringObjectMap = BeanUtil.beanToMap(menuVo, false, true);

//        获取除按钮外的全部菜单
        QueryWrapper<Menu> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.ne("menu_type", "F");
        List<Menu> menus=menuService.list(queryWrapper1);

        BaseUtils baseUtils=new BaseUtils();
        if (stringObjectMap.isEmpty()){
            return new ResponseBean(true, menus, UnicomResponseEnums.SUCCESS_OPTION);
        }
        if (menuVo.getParentId()==null){
            List<MenuVo> list =baseUtils.ArrangementMenuTree(menus, 0L);
            return new ResponseBean(true, list, UnicomResponseEnums.SUCCESS_OPTION);
        }
        QueryWrapper<Menu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id", menuVo.getParentId());
        IPage<Menu> iPageMenu=menuService.selectPage(menuVo);
        List<MenuVo> listMenus = new ArrayList<>();
        for (Menu m:iPageMenu.getRecords()) {
            listMenus.add(VoUtil.menuToMenuVo(m));
        }
        IPageVo<MenuVo> iPageVo = new IPageVo<>();
        System.out.println(iPageVo);

        iPageVo.setSize(iPageMenu.getSize());
        iPageVo.setCurrent(iPageMenu.getCurrent());
        iPageVo.setTotal(iPageMenu.getTotal());
        iPageVo.setPages(iPageMenu.getPages());
        List<MenuVo> menuVoList=baseUtils.ArrangementMenuTree(iPageMenu,menus);
        iPageVo.setRecords(menuVoList);
        return new ResponseBean(true, iPageVo, UnicomResponseEnums.SUCCESS_OPTION);
    }

}
